package com.mvc.business.user.intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.mvc.annotation.ClassAnnotation;
import com.mvc.annotation.ClassAnnotation.ClassAnnotationType;

/**
 * 拦劫配置了 自定义注解@ClassAnnotation的请求方法 
 * 在UserController的list请求中测试
 * @author HK
 *
 */
@Component
public class ClassAnnotationIntercepter extends HandlerInterceptorAdapter {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		if (handler.getClass().isAssignableFrom(HandlerMethod.class)) {
			ClassAnnotation myAnnotation = ((HandlerMethod) handler).getMethodAnnotation(ClassAnnotation.class);
			if (null == myAnnotation) {
				System.out.println("没有配置@ClassAnnotation");
			}
			//以下实现注解逻辑
			else if (myAnnotation.validate() == ClassAnnotationType.YES) {
				System.out.println("@ClassAnnotation中validate设置的值为 ClassAnnotationType.YES");
			}
			else {
				System.out.println("@ClassAnnotation中validate设置的值为 ClassAnnotationType.NO");
			}
		}
		return super.preHandle(request, response, handler);
	}
}
